// logs.js
const util = require('../../utils/util.js')
const client=require('../../utils/client.js')

Page({
  data: {
    status:'',
    isClose:'',
    doorNo:'',
    name:'',
    phone:'',
    idCard:'',
    zname:'',
    nphone:'',
    nidcardNo:'',
    unit:'',
    isRent:'2',
    isBack:'2',
    area:'',
    isCheck:'',
    isEnhance:'',
    isChildYM:'',
    isOlderYM:'',
    isIll:'2',
    readly:false,
    tabIndex:1,
    isShowSubmit:false,
    isShowDS:false,
    type:1,
    a:''
  },
  onLoad(options) {
    let _that = this
    let name=options.name||'';
    let m=options.m||'';
    let phone=options.phone||'';
    let card=options.card||'';
    let a=options.a

    let zname = options.zname || ''
    let nphone = options.nphone || ''
    let nidcardNo = options.nidcardNo || ''
    _that.setData({zname,nphone,nidcardNo})
    if(a){
      this.setData({a:a})
    }
    if(name){
      this.setData({name:name})
    }
    if(phone){
      this.setData({
        phone:phone
      })
    }
    if(card){
      this.setData({
        idCard:card
      })
    }
    console.log('m',m)
    if(m){
      let status;
      if(m=="红码"){
        status="3";
      }else if(m=="黄码"){
        status="2";
      }else if(m=="绿码"){
        status="1";
      }
      this.setData({status:status});
    }
  },
  jx(){
    let _that=this;
    this.setData({
      isShowSubmit:false
    });
    this.setData({
      status:'',
      isClose:'',
      name:'',
      phone:'',
      idCard:'',
      unit:'',
      isRent:'2',
      isBack:'2',
      area:'',
      isCheck:'',
      isEnhance:'',
      isChildYM:'',
      isOlderYM:'',
      isIll:'2',
      time:'',
      age:'',
      readly:false,
      tabIndex:1
    })
    wx.setStorageSync('isRent', 2)
    wx.setStorageSync('isClose', 2)
    wx.setStorageSync('isIll', 2)
    console.log('data',this.data)
    this.scaneCode()
  },
  scaneCode(){
    if(this.data.a==1){
      wx.showLoading({
        title: '加载中',
      })
      let _that=this;
      try{
        wx.scanCode({
          onlyFromCamera: true,
          fail:res=>{
            wx.navigateBack({
              delta: 1,
            })
          },
          success:res=>{
            let content=res.result;
            client.request('/app/wx/scan/v2','post',{'code':content},(data)=>{
              console.log(data);
              if(data.code==0){
                let info=data.data;

                let name=info.name;
                let m=info.status;
                let phone=info.phone;
                let card=info.card;
                console.log('info',info)

                _that.setData({name:name})
                _that.setData({phone:phone})
                _that.setData({idCard:card})
                _that.setData({zname:info.zname,nphone:info.nphone,nidcardNo:info.nidcardNo})

                

                if(m){
                  let status;
                  if(m=="红码"){
                    status="3";
                  }else if(m=="黄码"){
                    status="2";
                  }else if(m=="绿码"){
                    status="1";
                  }
                  _that.setData({status:status});
                  wx.showToast({
                    title: '扫码成功',
                    icon:'success'
                  })
                }
              }else{
                wx.showModal({
                  content: data.msg,showCancel:false
                })
              }
            })
          }
        })
      }finally{
        wx.hideLoading();
      }
    }
  },
  onShow(){
    let wzinfo=wx.getStorageSync("wzinfo");
    this.setData({addreess1:wzinfo})
    this.setData({type:wx.getStorageSync('type')})
    console.log('type',wx.getStorageSync('type'))
    let xz=wx.getStorageSync('xz');
    let cj=wx.getStorageSync('cj');
    let type=wx.getStorageSync('type');
    let dong=wx.getStorageSync('dong');
    let xq=wx.getStorageSync('xq');
    let danyuan=wx.getStorageSync('danyuan');
    let shi=wx.getStorageSync('shi');
    let kd=wx.getStorageSync('kd');
    let lu=wx.getStorageSync('lu');
    let hao=wx.getStorageSync('hao');
    let lat=wx.getStorageSync('lat');
    let long=wx.getStorageSync('long');
    this.setData({
      xz,
      cj,
      type,
      dong,
      xq,
      danyuan,
      shi,
      kd,
      lu,
      hao,
      lat,
      long,
      type
    })
    console.log('data',this.data)
  },
  saveBtn: function(e) {
      if(this.data.type==1){
        wx.setStorageSync('danyuan', this.data.danyuan)
        wx.setStorageSync('shi', this.data.shi)
      }else if(this.data.type==2){
        wx.setStorageSync('lu', this.data.lu)
        wx.setStorageSync('hao', this.data.hao)
      }
      this.setData({tabIndex:1,isShowDS:false})
      let wzinfo=wx.getStorageSync('xz')+wx.getStorageSync('cj');
      if(this.data.type=="1"){
        wzinfo+=this.data.xq+this.data.dong+"幢"+this.data.danyuan+"单元"+this.data.shi+"室";
      }else if(this.data.type=='2'){
        wzinfo+=this.data.lu+"路"+this.data.hao+"号";
      }else{
        wzinfo+=this.data.kd;
      }
      this.setData({addreess1:wzinfo})
      wx.setStorageSync('wzinfo', wzinfo);
      console.log('addresss',this.data.addreess1,wzinfo)
      this.scaneCode()
  },
  closeDS: function(e) {
    this.setData({
      isShowDS:false
    })
    wx.navigateBack({
      delta: 1,
    })
  },
  closeNext: function(e) {
    this.setData({
      isShowSubmit:false
    });
    wx.navigateBack();
  },
  nextBtn: function(e) {
    this.setData({
      isShowSubmit:false,
      isShowDS:true
    })
    this.setData({
      status:'',
      isClose:'',
      name:'',
      phone:'',
      idCard:'',
      isRent:'2',
      isBack:'2',
      area:'',
      isCheck:'',
      isEnhance:'',
      isChildYM:'',
      isOlderYM:'',
      isIll:'2',
      time:'',
      age:'',
      readly:false,
      tabIndex:1
    })
    wx.setStorageSync('isRent', 2)
    wx.setStorageSync('isClose', 2)
    wx.setStorageSync('isIll', 2)
  },
  clickTabItem: function(e) {
    console.log('clickItem',e.target.dataset.index)
    this.setData({
      tabIndex: e.target.dataset.index
    })
    console.log('tabIndex',this.data.tabIndex)
  },
  changeKd: function(e) {
    this.setData({
      kd: e.detail.value
    })
    console.log(this.data.kd)
  },
  changeDanyuan: function(e) {
    this.setData({
      danyuan: e.detail.value
    })
    console.log(this.data.danyuan)
  },
  changeShi: function(e) {
    this.setData({
      shi: e.detail.value
    })
    console.log(this.data.shi)
  },
  changeLu: function(e) {
    this.setData({
      lu: e.detail.value
    })
    console.log(this.data.lu)
  },
  changeHao: function(e) {
    this.setData({
      hao: e.detail.value
    })
    console.log(this.data.hao)
  },
  changeIsRent: function(e) {
    this.setData({
      isRent: e.detail.value
    })
    wx.setStorageSync('isRent', e.detail.value)
    console.log(this.data.isRent)
  },
  changeIsIll: function(e) {
    this.setData({
      isIll: e.detail.value
    })
    console.log(this.data.isIll)
  },
  changeIsOlderYM: function(e) {
    this.setData({
      isOlderYM: e.detail.value
    })
    console.log(this.data.isOlderYM)
  },
  changeIsChildYM: function(e) {
    this.setData({
      isChildYM: e.detail.value
    })
    console.log(this.data.isChildYM)
  },
  changeIsEnhance: function(e) {
    this.setData({
      isEnhance: e.detail.value
    })
    console.log(this.data.isEnhance)
  },
  changeCheck: function(e) {
    this.setData({
      isCheck: e.detail.value
    })
    console.log(this.data.isCheck)
  },
  changeStatus: function(e) {
    if(this.data.a!=1){
      this.setData({
        status: e.detail.value
      })
      console.log(this.data.status)
    }
  },
  changeIsClose: function(e) {
    console.log('e',e)
    this.setData({
      isClose: e.detail.value
    })
    console.log(this.data.isClose)
  },
  bindAgeInput: function(e) {
    this.setData({
      age: e.detail.value.replace(/\s*/g,"")
    })
    console.log(this.data.age)
  },
  bindNameInput: function(e) {
    this.setData({
      name: e.detail.value.replace(/\s*/g,"")
    })
    console.log(this.data.name)
  },
  changeRent: function(e) {
    this.setData({
      isRent: e.detail.value
    })
    console.log(this.data.isRent)
  },
  changeBack: function(e) {
    this.setData({
      isBack: e.detail.value
    })
    console.log(this.data.isBack)
  },
  bindPhone(e){
    this.setData({
      phone:e.detail.value.replace(/\s*/g,"")
    })
  },
  bindIdCard(e){
    this.setData({
      idCard:e.detail.value.replace(/\s*/g,"")
    })
  },

  /**
   * 表单提交
   */
  saveData: function(e) {
    let _this = this;
    let form=e.detail.value;
    var values = {
      name:this.data.name,
      phone:this.data.phone,
      idCard:this.data.idCard,
      isBack:this.data.isBack,
      isIll:this.data.isIll,
      area:form.area
    };
    // 表单验证
    if(_this.data.tabIndex==1){
      if(_this.data.type==3){
        console.log('验证卡点')
        if (!_this.validationKD(values)) {
          wx.showModal({
            title:'友情提示',
            content: _this.data.error,
            showCancel: false
          });
          return false;
        }
      }else{
        if (!_this.validation(values)) {
          wx.showModal({
            title:'友情提示',
            content: _this.data.error,
            showCancel: false
          });
          return false;
        }
      }
    }else{
      if (!_this.validation2(values)) {
        wx.showModal({
          title:'友情提示',
          content: _this.data.error,
          showCancel: false
        });
        return false;
      }
    }
    
    let nstatus;
    if(values.status=="1"){
      nstatus="绿码";
    }else if(values.status=="2"){
      nstatus="黄码"
    }else {
      nstatus="红码"
    }
    values.status=nstatus;
    let xz=wx.getStorageSync('xz');
    let cj=wx.getStorageSync('cj');
    let type=wx.getStorageSync('type');
    let dong=wx.getStorageSync('dong');
    let xq=wx.getStorageSync('xq');
    let danyuan=wx.getStorageSync('danyuan');
    let shi=wx.getStorageSync('shi');
    let kd=wx.getStorageSync('kd');
    let lu=wx.getStorageSync('lu');
    let hao=wx.getStorageSync('hao');
    let wzinfo=wx.getStorageSync('wzinfo');
    let lat=wx.getStorageSync('lat');
    let long=wx.getStorageSync('long');
    let ld=wx.getStorageSync('ld');
    let isClose=wx.getStorageSync('isClose')||2;
    let isRent=wx.getStorageSync('isRent')||2;

    let address1=type=="1"?xq:lu;
    if(type=="3"){
      address1=kd;
    }
    console.log(values);
    let address2=type=="2"?hao:dong;
    let card=this.data.tabIndex==1?values.idCard:this.data.age;
    let data={
      status:this.data.status,
      xz:xz,
      cj:cj,
      addressType:type,
      address1:address1,
      address2:address2,
      address3:danyuan,
      unit:shi,
      userType:this.data.tabIndex,
      color:this.data.status,
      name:this.data.a==1?this.data.zname:values.name,
      phone:this.data.a==1?this.data.nphone:values.phone,
      isBack:values.isBack,
      area:values.area,
      idCard:this.data.a==1?this.data.nidcardNo:card,
      isClose:isClose,
      isRent:isRent,
      isIll:values.isIll,
      lat:_this.data.lat,
      laong:_this.data.long,
      type:1
    }
    client.request('/app/wx/inp','post',data,(d)=>{
        if(d.code==0){
          this.setData({isShowSubmit:true})
        }else{
          wx.showModal({
            content: d.msg,
            showCancel: false
          });
        }
    })

  },

  /**
   * 表单验证
   */
  validation: function(values) {
    console.log(values);
    if(this.data.a!=1){
      if (values.name == '') {
        this.data.error = '姓名不能为空';
        return false;
      }
      if (values.phone.length < 1) {
        this.data.error = '联系方式不能为空';
        return false;
      }
      let reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
      if (!reg.test(values.phone)) {
        this.data.error = '联系方式格式不符合要求';
        return false;
      }
      if (values.idCard.length < 1) {
        this.data.error = '身份证号不能为空';
        return false;
      }
      if (values.idCard.length != 18) {
        this.data.error = '身份证号长度有误';
        return false;
      }
      let regIdCard = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
      if (!regIdCard.test(values.idCard)) {
        this.data.error = '身份证号格式有误';
        return false;
      }
    }
    
    if(values.isBack==''){
      this.data.error = '是否为重点地区返苍人员不能为空';
      return false;
    }
    if(values.isBack==1){
      if(!values.area){
        this.data.error = '请填写返苍前所在地区';
        return false;
      }
    }
    if (values.isIll=='') {
      this.data.error = '是否有行动不便不能为空';
      return false;
    }
    // if (values.isRent=='') {
    //   this.data.error = '是否为出租房不能为空';
    //   return false;
    // }
    // if (this.data.isRent=='') {
    //   this.data.error = '是否为出租房不能为空';
    //   return false;
    // }
    return true;
  },
  validationKD: function(values) {
    console.log(values);
    if(this.data.a!=1){
      if (values.name == '') {
        this.data.error = '姓名不能为空';
        return false;
      }
      if (values.phone.length < 1) {
        this.data.error = '联系方式不能为空';
        return false;
      }
      let reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
      if (!reg.test(values.phone)) {
        this.data.error = '联系方式格式不符合要求';
        return false;
      }
      if (values.idCard.length < 1) {
        this.data.error = '身份证号不能为空';
        return false;
      }
      if (values.idCard.length != 18) {
        this.data.error = '身份证号长度有误';
        return false;
      }
    }
    
    if(values.isBack==''){
      this.data.error = '是否为重点地区返苍人员不能为空';
      return false;
    }
    if(values.isBack==1){
      if(!values.area){
        this.data.error = '请填写返苍前所在地区';
        return false;
      }
    }
    return true;
  },
  validation2: function(values) {
    if (values.name == '') {
      this.data.error = '姓名不能为空';
      return false;
    }
    if (values.age == '') {
      this.data.error = '年龄不能为空';
      return false;
    }
    return true;
  },
})
